<?PHP

class WSRequest {
	var $wsanswer;

	function WSRequest($api, $action, $item=0, $catid=0, $date=0, $time=0) {
		//$url="http://www.bigbusnyc.com/ws/index.php";
		$url="http://servak2/~dima/csny/ws/index.php";
		
		$xmlsrc  = "<?xml version=\"1.0\" encoding=\"UTF8\"?>\r\n";
		$xmlsrc .= "<XMLRequest>\r\n\t";
		$xmlsrc .= "<ApiKey>".$api."</ApiKey>\r\n\t";
		$xmlsrc .= "<Action>".strtoupper($action)."</Action>\r\n\t";
		if (!empty($item)) $xmlsrc .= "<ItemCode>".strtoupper($item)."</ItemCode>\r\n\t";
		if (!empty($catid)) $xmlsrc .= "<CategoryId>".strtoupper($catid)."</CategoryId>\r\n\t";
		if (!empty($date)) $xmlsrc .= "<Date>".strtoupper($date)."</Date>\r\n\t";
		if (!empty($time)) $xmlsrc .= "<Time>".strtoupper($time)."</Time>\r\n\t";
		$xmlsrc .= "</XMLRequest>";
		
//echo $url."?xml=".rawurlencode($xmlsrc)."\r\n";
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_URL,$url."?xml=".rawurlencode($xmlsrc));
		curl_setopt($ch, CURLOPT_TIMEOUT, 120);
		curl_setopt($ch, CURLOPT_LOW_SPEED_LIMIT, 1);
		curl_setopt($ch, CURLOPT_LOW_SPEED_TIME, 60);
		curl_exec ($ch);
		$this->wsanswer=curl_multi_getcontent($ch);
		curl_close ($ch);
	}
}


class XMLprocess {
	var $doc;
	var $Status; // OK or ERROR
	var $ErrorCode;//string
	var $ErrorMessage; //string
	var $aCategorys;  //array ---------
	var $ItemTitle;  //array ---------
	var $aItems;    //array ---------
	var $Warning;	//string
	
	var $Error;
	
	function XMLprocess($xml_input) {
	
	$this->doc  = new DOMDocument(); 
        $this->doc->loadXML($xml_input);
        $answer= $this->doc->getElementsByTagName("XMLResponce");
//echo($xml_input);
	$tags=$answer->item(0)->childNodes;
        foreach ($tags as $tag)
             $this->{$tag->nodeName}=$tag->nodeValue;


	//For array of items
	$Items = $this->doc->getElementsByTagName("Item"); //<Item>
	foreach ($Items as $Item) {
            $ItemChilds = $Item->childNodes;
            foreach ($ItemChilds as $Child){
                if (strpos($Child->nodeName,"Code")) $key = $Child->nodeValue;
                $aChild[$Child->nodeName]=$Child->nodeValue;
            }
            $this->aItems[$aChild["Code"]]=$aChild;
        }

        //For array of categorys
	$Items = $this->doc->getElementsByTagName("Category"); //<Item>
	foreach ($Items as $Item) {
            $ItemChilds = $Item->childNodes;
            foreach ($ItemChilds as $Child){
                $aChild[$Child->nodeName]=$Child->nodeValue;
            }
            $this->aCategorys[$aChild["Id"]]=$aChild;
        }


        
	
	}
	
	function checktag($tagname){
	if (isset($this->doc->getElementsByTagName($tagname)->item(0)->nodeValue)) 
		$this->{$tagname} = $this->doc->getElementsByTagName($tagname)->item(0)->nodeValue;
         //echo $tagname."__".$this->doc->getElementsByTagName($tagname)->item(0)->nodeValue."==";
        
	}
}

// ============================================
//  You can uncomment one of requests examples
// ============================================
//
//$request = new WSRequest("123123123", "PING");
//$request = new WSRequest("123123123", "ITEMS_LIST");
//$request = new WSRequest("123123123", "ITEM_DETAILS", "NYWC");
//$request = new WSRequest("123123123", "CATEGORY_LIST");
//$request = new WSRequest("123123123", "CATEGORY_CONTENT", 0,8);
//$request = new WSRequest("123123123", "ITEM_AVAILABILITY", "NYWC" ); //without date and time
//$request = new WSRequest("123123123", "ITEM_AVAILABILITY", "NYWC", "09.01.2009", "16:40"); //date format MM.DD.YYYY   time format HH:MM

// ======================== 
// Next string can show you XML answer for you request
//echo $request->wsanswer;


// ======================== 
// Next string parse XML answer into object
//$XMLprocessed= new XMLprocess($request->wsanswer);

// Next string show object that have response in fine readeble format.
//var_dump($XMLprocessed);

//===============================================================
// You can use Classes WSRequest and XMLprocess in You php code
//===============================================================

?>